home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.10 Oct 94 / Sprocket / AppSpecific / DocWindow.cp < prev    next >
Encoding:
Text File  |  1994-08-25  |  5.3 KB  |  276 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DocWindow.cp
  3.  
  4.     Contains:    A simple document window
  5.                 
  6.     Written by: Dave Falkenburg
  7.     
  8.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.  
  13.     To Do:        Figure out if any of these methods should move to TWindow
  14.  
  15.  */
  16.  
  17. #include "DocWindow.h"
  18. #include "AppLib.h"
  19. #include <LowMem.h>        //    for LMGetCurApName()
  20. #include <ToolUtils.h>    //    for NumToString()
  21. #include <Icons.h>
  22. #include <Threads.h>
  23.  
  24. unsigned long    TDocWindow::fgUntitledTagCount = 0;
  25.  
  26.  
  27. const short        kHeaderHeight = 20;
  28. const short        kScrollbarWidth = 15;
  29.  
  30. const short        kFirstSpinningArrowIconID = 801;
  31. const short        kLastSpinningArrowIconID = 808;
  32.  
  33. const short        kDocWindowTemplateID = 1026;
  34.  
  35.  
  36. static void
  37. SpinArrowsThreadProc(TDocWindow * windowToSpin)
  38.     {
  39.     while (1)
  40.         {
  41.         windowToSpin->SpinHeaderArrows();
  42.         YieldToAnyThread();
  43.         }
  44.     }
  45.     
  46.  
  47. static    void
  48. MyDrawGrowIcon(WindowPtr aWindow)
  49.     {
  50.     Rect        r = aWindow->portRect;
  51.     RgnHandle    savedClip = NewRgn();
  52.     
  53.     GetClip(savedClip);
  54.  
  55.     r.top += kHeaderHeight;
  56.     ClipRect(&r);
  57.     DrawGrowIcon(aWindow);
  58.     SetClip(savedClip);
  59.     DisposeRgn(savedClip);    
  60.     }
  61.  
  62.  
  63. TDocWindow::TDocWindow()
  64.     {
  65.     OSErr    err;
  66.  
  67.     fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
  68.  
  69.     TDocWindow::fgUntitledTagCount++;            //    Starts out as zero but we want “Untitled-1”
  70.     this->CreateWindow();
  71.     
  72.     if (gHasThreadManager)
  73.         {
  74.         err = NewThread(kCooperativeThread,(ThreadEntryProcPtr) SpinArrowsThreadProc,this,0,0,nil,&fSpinnerThreadID);
  75.         if (err != noErr)
  76.             DebugStr((StringPtr) "\pNewThread failed");
  77.         }
  78.     }
  79.  
  80. TDocWindow::~TDocWindow()
  81.     {
  82.     OSErr    err;
  83.  
  84.     //    NEWS OF THE WEIRD!
  85.     //    If you pass true for recyleThread param, the heap block for the stack isn’t
  86.     //    disposed— it gets placed in the default cooperative thread pool of “premade”
  87.     //    threads.
  88.     //
  89.     //    To optimize memory usage, I might just want to go ahead and create
  90.     //    a thread pool at application startup so we can avoid moving memory around
  91.     //    so much.
  92.     
  93.     if (gHasThreadManager)
  94.         {
  95.         err = DisposeThread(fSpinnerThreadID,nil,false);
  96.         if (err != noErr)
  97.             DebugStr((StringPtr) "\pDisposeThread failed");
  98.         }
  99.     }
  100.  
  101.     
  102. WindowPtr
  103. TDocWindow::MakeNewWindow(WindowPtr behindWindow)
  104.     {
  105.     WindowPtr    aWindow = GetNewColorOrBlackAndWhiteWindow(kDocWindowTemplateID,nil,behindWindow);
  106.     Str255        titleString;
  107.     
  108.     if (aWindow)
  109.         {
  110.         //    make the window exciting & different
  111.         
  112.         //    Can’t call nudge because it assumes the window is visible!
  113.         
  114.         //    Nudge(fgUntitledTagCount * kHeaderHeight,fgUntitledTagCount * kHeaderHeight);
  115.     
  116.         GetWTitle(aWindow,titleString);
  117.         if (StrLength(titleString) != 0)
  118.             {
  119.             Str255 numberString;
  120.             
  121.             NumToString(fgUntitledTagCount,numberString);
  122.             BlockMove(&numberString[1],&titleString[titleString[0]+1],numberString[0]);
  123.             titleString[0] += numberString[0];
  124.             }
  125.         
  126.         SetWTitle(aWindow,titleString);
  127.         ShowWindow(aWindow);
  128.         }
  129.  
  130.     return aWindow;
  131.     }
  132.  
  133.  
  134. void
  135. TDocWindow::Activate(Boolean /* activating */)
  136.     {
  137.     MyDrawGrowIcon(fWindow);
  138.     }
  139.  
  140.  
  141. void
  142. TDocWindow::AdjustCursor(EventRecord * /* anEvent */)
  143.     {
  144.     }
  145.  
  146.  
  147. void
  148. TDocWindow::Draw(void)
  149.     {
  150.     EraseRect(&fWindow->portRect);
  151.  
  152.     MoveTo(0,kHeaderHeight-3); LineTo(fWindow->portRect.right,kHeaderHeight-3);
  153.     MoveTo(0,kHeaderHeight-1); LineTo(fWindow->portRect.right,kHeaderHeight-1);
  154.         
  155.     MyDrawGrowIcon(fWindow);
  156.     }
  157.  
  158.     
  159. void
  160. TDocWindow::Click(EventRecord * /* anEvent */)
  161.     {
  162.     this->Select();
  163.     }
  164.  
  165.  
  166. void
  167. TDocWindow::AdjustForNewWindowSize(Rect *oldSize,Rect * /* newSize */)
  168.     {
  169.     Rect    scrollbarRect;
  170.     
  171.     //    Invalidate the old vertical scroll bar
  172.     
  173.     scrollbarRect.top = oldSize->top;
  174.     scrollbarRect.left = oldSize->right - kScrollbarWidth;
  175.     scrollbarRect.bottom = oldSize->bottom;
  176.     scrollbarRect.right = oldSize->right;
  177.     InvalRect(&scrollbarRect);
  178.  
  179.     //    Invalidate the old horizontal scroll bar
  180.  
  181.     scrollbarRect.left = oldSize->left;
  182.     scrollbarRect.top = oldSize->bottom - kScrollbarWidth;
  183.     InvalRect(&scrollbarRect);
  184.  
  185.     MyDrawGrowIcon(fWindow);
  186.     }
  187.  
  188.  
  189. Boolean
  190. TDocWindow::Close(void)
  191.     {
  192.     StandardCloseResult    result;
  193.     Str255                title;
  194.     
  195.     GetWTitle(this->fWindow,title);
  196.     result = StandardCloseDocument(LMGetCurApName(),title,false,false);
  197.  
  198.     if (result != kCancelSaveDocument)
  199.         {
  200.         return TWindow::Close();
  201.         }
  202.     return false;
  203.     }
  204.  
  205.  
  206. OSErr
  207. TDocWindow::DragEnterWindow(DragReference theDrag)
  208.     {
  209.     return this->DragInWindow(theDrag);
  210.     }
  211.  
  212.  
  213. OSErr
  214. TDocWindow::DragInWindow(DragReference theDrag)
  215.     {
  216.     RgnHandle    hiliteRgn = NewRgn();
  217.     Point        mouseLoc, pinnedMouseLoc;
  218.     
  219.     SetRectRgn( hiliteRgn,
  220.                 fWindow->portRect.left,
  221.                 fWindow->portRect.top + kHeaderHeight,
  222.                 fWindow->portRect.right-kScrollbarWidth,
  223.                 fWindow->portRect.bottom-kScrollbarWidth);
  224.  
  225.     (void) GetDragMouse(theDrag,&mouseLoc,&pinnedMouseLoc);
  226.     GlobalToLocal(&mouseLoc);
  227.     
  228.     if (PtInRgn(mouseLoc,hiliteRgn))
  229.         ShowDragHilite(theDrag,hiliteRgn,true);
  230.     else
  231.         HideDragHilite(theDrag);
  232.         
  233.     DisposeRgn(hiliteRgn);    
  234.     return noErr;
  235.     }
  236.  
  237.  
  238. OSErr
  239. TDocWindow::DragLeaveWindow(DragReference theDrag)
  240.     {
  241.     HideDragHilite(theDrag);
  242.     return noErr;
  243.     }
  244.  
  245.  
  246. OSErr
  247. TDocWindow::HandleDrop(DragReference /* theDrag */)
  248.     {
  249.     return noErr;
  250.     }
  251.  
  252.  
  253. void
  254. TDocWindow::SpinHeaderArrows()
  255.     {
  256.     GrafPtr    oldPort;
  257.     
  258.     GetPort(&oldPort);
  259.     SetPort(fWindow);
  260.     
  261.     Rect    r;
  262.     
  263.     fCurrentSpinningArrowIconID++;
  264.     
  265.     if (fCurrentSpinningArrowIconID > kLastSpinningArrowIconID)
  266.         fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
  267.     
  268.     r.top = fWindow->portRect.top;
  269.     r.left = fWindow->portRect.left+4;
  270.     r.bottom = r.top + 16;
  271.     r.right = r.left + 16;
  272.     (void) PlotIconID(&r,atNone,ttNone,fCurrentSpinningArrowIconID);
  273.  
  274.     SetPort(oldPort);
  275.     }
  276.